﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using DataAccess;
using System.Data;
using System.Data.Common;

/// <summary>
/// Summary description for ajax
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class ajax : System.Web.Services.WebService {

    public ajax () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod (EnableSession = true)]
    public string checkLogin(string Username, string Password, string Table)
    {
        string Fullname = "";
        string Mobile = "";
        string Email = "";
        string Address = "";
        string ManagerID = "";
        string AdminPassword = "";

        string md5password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");
        DbCommand cmd = GenericData.CreateCommand();

        cmd.CommandText = "SELECT Username, Password, Fullname, ID, Mobile, Email, Address FROM " + Table + " Where Username = '" + Username + "' and Password = '" + md5password + "'";
        DataTable table = GenericData.ExecuteReader(cmd);

        //load config
        cmd.CommandText = "SELECT * FROM Config WHERE ID = 1";
        DataTable tbl = GenericData.ExecuteReader(cmd);


        if (table.Rows.Count > 0)
        {
            Fullname = table.Rows[0]["Fullname"].ToString();
            ManagerID = table.Rows[0]["ID"].ToString();
            Mobile = table.Rows[0]["Mobile"].ToString();
            Address = table.Rows[0]["Address"].ToString();
            Email = table.Rows[0]["Email"].ToString();
            
            AdminPassword = table.Rows[0]["Password"].ToString();
            Session.Timeout = 3600*24;

            if (tbl.Rows.Count > 0)
            {
                Session.Add("PageSize", tbl.Rows[0]["PageSize"].ToString());
                Session.Add("PageName", tbl.Rows[0]["PageName"].ToString());
            }

            if (Table == "Manager")
            {
                Session.Add("Admin", Username);
                Session.Add("ManagerID", ManagerID);
                Session.Add("AdminName", Fullname);
                Session.Add("AdminPassword", AdminPassword);
            }
            else
            {
                Session.Add("Username", Username);
                Session.Add("Fullname", Fullname);
                Session.Add("Mobile", Mobile);
                Session.Add("Address", Address);
                Session.Add("Email", Email);
                Session.Add("CandidateID", table.Rows[0]["ID"].ToString());
                
            }
            return "success";
        }
        else
            return "failed";
    }

    [WebMethod(EnableSession = true)]
    public bool logout()
    {
        Session.RemoveAll();
        return true;
    }

    [WebMethod(EnableSession = true)]
    public void DeleteCategory(int CategoryID)
    {
        CategoryDAL catedal = new CategoryDAL();
        if (catedal.Delete(CategoryID))
            Session.Add("Message", "Delete CategoryID : " + CategoryID + " successfully");
        else
            Session.Add("Message", "<font color='red'>Error : Delete CategoryID : " + CategoryID + " unsuccess</font>");
    }

    [WebMethod(EnableSession = true)]
    public void DeleteQuestion(int QuestionID)
    {
        QuestionDAL question = new QuestionDAL();
        if (question.Delete(QuestionID))
            Session.Add("Message", "Delete QuestionID : " + QuestionID + " successfully");
        else
            Session.Add("Message", "<font color='red'>Error : Delete QuestionID : " + QuestionID + " unsuccess</font>");
    }
    
}
